var http = require('../../utils/http.js');
var config = require('../../utils/config.js');
Page({
  data: {
    userInfo: {
      encryptedData: '',
      iv: '',
      openid: '',
      session_key: '',
      nickName: '微信用户',
      avatarUrl: 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0',
      codeid:'',
      phone:'',
      accessToken:'',
      refreshToken:'',
    },
    step:0,
  },
  onLoad() {
    if (wx.getUserProfile) {

    }
  },
  getUserProfile:function(e) {
    // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认
    // 开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
    let that = this
    wx.getUserProfile({
      desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        if (res.errMsg === "getUserProfile:ok") {
          that.userInfo={...res.userInfo};
          that.setData({
            userInfo: that.userInfo
          });
          wx.login({
            success(res) {
              console.log(res)
              http.request({
                url: "/wxlogin",
                method: "POST",
                data: {
                  userName: that.userInfo.nickName,
                  pic: that.userInfo.avatarUrl,
                  codeid: res.code,
                  passWord: 1
                },
                callBack: (res) => {
                  that.userInfo.openid=res.openid;
                  that.userInfo.session_key=res.session_key;
                  that.setData({
                    userInfo:that.userInfo,
                    step: 1
                  });
                }
              })
            }
          });
        } else {
          wx.showToast({
            title: "获取权限失败",
            icon: "none"
          })
        }
      },
      complete: (res) => {
        if (res.errMsg === "getUserProfile:fail auth deny") { 
          wx.showToast({
            title: "获取权限失败",
            icon: "none"
          })
        }
      },
    })
  },
  getPhoneNumber: function (e) {
    if (e.detail.errMsg == "getPhoneNumber:ok") {
      //用户按了允许授权按钮
      var that = this;
      wx.login({
        success(loginRes) {
          console.log(loginRes)
          if (loginRes.code) {
            that.userInfo.codeid=loginRes.code;
            that.userInfo.iv=e.detail.iv;
            that.userInfo.encrypteData=e.detail.encryptedData;
            console.log(that.userInfo);
            // 调用你的登录接口将用户收据发送给服务器
            http.request({
              url: "/wxphone",
              method: "POST",
              data: that.userInfo,
              callBack: (res) => {
                that.userInfo.nickName='';
                that.userInfo.phone=res.phone;
                that.userInfo.accessToken=res.accessToken;
                that.userInfo.refreshToken=res.refreshToken;
                console.log(that.userInfo);
                that.setData({
                  userInfo:that.userInfo,
                  step:2,
                });
              }
            })
          }
        }
      })
    }
    else {
      wx.showToast({
        title: "获取权限失败",
        icon: "none"
      })
    }
  },
  onChooseAvatar(e) {
    this.userInfo.avatarUrl=e.detail.avatarUrl;
    this.setData({
      userInfo:this.userInfo,
    });
    console.log(this.userInfo);
  },
  onNickname(e){
    this.userInfo.nickName=e.detail.value;
  },
  updateInfo(){
    var that=this;
    console.log(this.userInfo);
    if(!this.userInfo.nickName){
      wx.showToast({
        title: '昵称不可为空',
        icon:"none"
      });
      return;
    }
    http.request({
      url: "/wxupdate",
      method: "POST",
      data: that.userInfo,
      callBack: (res) => {
        wx.setStorageSync('userInfo', that.userInfo);
        wx.setStorageSync('token', that.userInfo.accessToken);
        that.setData({
          userInfo:that.userInfo,
          step:0,
        });
        wx.switchTab({
          url: '/pages/index/index',
        });
      }
    })
  }
})